package com.lyn.watches;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class GaoDaWatchesView extends View {
    private Paint paint;
    private Context context;
    private Bitmap bg_bitmap;
    private Bitmap rain_day_Bitmap;
    private Bitmap snow_day_Bitmap;
    private Bitmap suny_day_Bitmap;
    private Bitmap weather_no_data_Bitmap;
    private Bitmap battery_Bitmap;
    //体温
    private String bodyTemperature;
    //下雨天
    public static final int RAINY_DAY = 0;
    //下雪天
    public static final int SNOWING_DAY = 1;
    //晴天
    public static final int SUNNY_DAY = 2;
    //没有数据
    public static final int NO_WEATHER_DATA = 3;
    //天气类型
    private volatile int weatherType;
    //天气温度
    private volatile String weatherTemperature;
    //心率
    private volatile String heartRate;
    //步数
    private volatile String stepCount;
    //电量
    private volatile String electricity;
    //卡路里
    private volatile String calories;
    //fir
    private volatile String fri;
    //时间
    private volatile String time;
    //月日
    private volatile String monthDay;

    public GaoDaWatchesView(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public GaoDaWatchesView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(10);
        paint.setTextSize(50);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setColor(Color.WHITE);
        Typeface typeface = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
        paint.setTypeface(typeface);
        bg_bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_gundam_background, null);


        rain_day_Bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.rain_day, null);
        snow_day_Bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.snow_day, null);
        suny_day_Bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.suny_day, null);
        weather_no_data_Bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.weather_no_data, null);
        battery_Bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_gundam_battery, null);

    }

    /**
     * 设置天气类型
     *
     * @param weatherType
     */
    public void setWeatherType(int weatherType) {
        this.weatherType = weatherType;
        postInvalidate();
    }

    /**
     * 设置体温
     * @param bodyTemperature
     */
    public void setBodyTemperature(String bodyTemperature) {
        this.bodyTemperature = bodyTemperature;
        postInvalidate();
    }

    /**
     * 设置天气温度
     * @param weatherTemperature
     */
    public void setWeatherTemperature(String weatherTemperature) {
        this.weatherTemperature = weatherTemperature;
        postInvalidate();
    }

    /**
     * 设置心率
     * @param heartRate
     */
    public void setHeartRate(String heartRate) {
        this.heartRate = heartRate;
        postInvalidate();
    }

    /**
     * 设置步数
     * @param stepCount
     */
    public void setStepCount(String stepCount) {
        this.stepCount = stepCount;
        postInvalidate();
    }

    /**
     * 设置电量
     * @param electricity
     */
    public void setElectricity(String electricity) {
        this.electricity = electricity;
        postInvalidate();
    }

    /**
     * 设置卡路里
     * @param calories
     */
    public void setCalories(String calories) {
        this.calories = calories;
        postInvalidate();
    }

    /**
     * 设置fir
     * @param fri
     */
    public void setFri(String fri) {
        this.fri = fri;
        postInvalidate();
    }

    /**
     * 设置时分
     * @param time
     */
    public void setTime(String time) {
        this.time = time;
        postInvalidate();
    }

    /**
     * 设置月日
     * @param monthDay
     */
    public void setMonthDay(String monthDay) {
        this.monthDay = monthDay;
        postInvalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawBitmap(bg_bitmap, null, new Rect(0, 0, canvas.getWidth(), canvas.getHeight()), paint);

        int paddingLeft = 180;
        int paddingTop = 120;
        int left = paddingLeft;
        int top = paddingTop;
        int right = canvas.getWidth() - left;
        int bottom = canvas.getHeight() - top;

        int centerX = (right - left) / 2 + left;
        int centerY = (bottom - top) / 2 + top;

        paint.setTextSize(50);
        paint.setColor(Color.WHITE);
        canvas.drawText(bodyTemperature, left, baseLine(top), paint);

        canvas.drawText(heartRate, right, baseLine(top), paint);

        canvas.drawText(stepCount, left, baseLine(bottom), paint);

        canvas.drawText(calories, right, baseLine(bottom), paint);

        int iconSize = 55;
        Rect weatherRect = new Rect();
        weatherRect.left = centerX - iconSize;
        weatherRect.top = iconSize;
        weatherRect.right = centerX + iconSize;
        weatherRect.bottom = weatherRect.top + iconSize + iconSize;

        switch (weatherType) {
            case RAINY_DAY:
                canvas.drawBitmap(rain_day_Bitmap, null, weatherRect, paint);
                break;
            case SNOWING_DAY:
                canvas.drawBitmap(snow_day_Bitmap, null, weatherRect, paint);
                break;
            case SUNNY_DAY:
                canvas.drawBitmap(suny_day_Bitmap, null, weatherRect, paint);
                break;
            default:
                canvas.drawBitmap(weather_no_data_Bitmap, null, weatherRect, paint);
                break;
        }

        canvas.drawText(weatherTemperature, centerX, weatherRect.bottom + 70, paint);

        Rect BatteryRect = new Rect();
        BatteryRect.left = centerX - iconSize;
        BatteryRect.bottom = canvas.getHeight() - iconSize*2;
        BatteryRect.top = BatteryRect.bottom-iconSize*2;
        BatteryRect.right = centerX + iconSize;

        canvas.drawBitmap(battery_Bitmap, null, BatteryRect, paint);
        canvas.drawText(electricity, centerX, BatteryRect.bottom + 70, paint);

        int paddingY = 230;
        paint.setTextSize(60);
        canvas.drawText(fri, centerX, baseLine(centerY - paddingY), paint);

        canvas.drawText(monthDay, centerX, baseLine(centerY) + paddingY, paint);

        paint.setTextSize(200);
        paint.setColor(context.getResources().getColor(R.color.teal_200));
        canvas.drawText(time, centerX, baseLine((bottom - top) / 2 + top), paint);

        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(30);
        canvas.drawRect(new Rect(0,0,canvas.getWidth(),canvas.getHeight()),paint);
    }


    private float baseLine(int y) {
        //计算baseline
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        float baseline = y + distance;
        return baseline;
    }
}
